package com.dpteam.controller;

import com.dpteam.dao.CategoryDao;
import com.dpteam.model.Category;
import com.dpteam.service.ProductService;
import com.dpteam.service.StatisticsService;
import com.dpteam.util.Constant;
import com.dpteam.util.OptionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: luolei
 * Date: 2016/1/13
 * Time: 16:53
 */
@Controller
@RequestMapping("statistics")
public class StatisticsController {

    @Resource
    private StatisticsService statisticsService;

    @Resource
    private ProductService productService;

    private static Logger logger = LoggerFactory.getLogger(StatisticsController.class);

    /**
     * 指定年份商品入库总数量查询
     * @param map
     * @return
     */
    @RequestMapping("index")
    public String index(ModelMap map,@RequestParam(defaultValue = "2015") int year) {
        logger.info("{}年商品入库总数量查询", year);
        String option1 = statisticsService.statisticsImportNumber(year);
        List<Category> categories = productService.findAllCategory();
        map.put(Constant.categories, categories);
        map.put(OptionUtil.option + "1", option1);

        String option2 = statisticsService.statisticsProfit(year);
        map.put(OptionUtil.option + "2", option2);
//        System.out.println(option2);
        String option3 = statisticsService.statisticsProfitPie(year);
        map.put(OptionUtil.option + "3", option3);
        System.out.println(option3);
        return "orderManage";
    }

    @RequestMapping(value = "importYearCategory", method = RequestMethod.POST)
    @ResponseBody public String importYearCategory(@RequestParam(defaultValue = "2015") int year,@RequestParam(defaultValue = "0") int categoryId,@RequestParam(defaultValue = "1") int type) {
        logger.info("统计指定年份，指定商品分类1-12月进货数量图");
        String option;
        if (categoryId == 0) {
            if (type == 1) {
                option = statisticsService.statisticsImportNumber(year);
            } else {
                option = statisticsService.statisticsImportCost(year);
            }

        } else {
            if (type == 1) {
                option = statisticsService.statisticsImportNumber(year, categoryId);
            } else {
                option = statisticsService.statisticsImportCost(year, categoryId);
            }
        }
        return option;
    }

    @RequestMapping("sumSale")
    @ResponseBody public String sumSale(@RequestParam(defaultValue = "2015") int year ,@RequestParam(defaultValue = "1") int allOrOne,@RequestParam(defaultValue = "1") int profitOrSell) {
        logger.info("销售统计");
        String option;
        if (allOrOne == 1) {
            //所有商品统计
            if (profitOrSell == 1) {
                //利润统计
                option = statisticsService.statisticsProfit(year);
            } else {
                //营业额统计
                option = statisticsService.statisticsTurnover(year);
            }
        } else {
            //商品分类统计
            if (profitOrSell == 1) {
                //利润统计
                option = statisticsService.statisticProductProfit(year);
            } else {
                //营业额统计
                option = statisticsService.statisticProductTurnover(year);
            }
        }
        return option;
    }

    @RequestMapping("sumSalePie")
    @ResponseBody public String sumSalePie(@RequestParam(defaultValue = "2015") int year,@RequestParam(defaultValue = "0") int month ,@RequestParam(defaultValue = "1") int profitOrSell) {
        logger.info("统计饼图");
        String option;
        if (month == 0) {
            //统计全年
            if (profitOrSell == 1) {
                //统计利润
                option = statisticsService.statisticsProfitPie(year);
            } else {
                //统计营业额
                option = statisticsService.statisticsTurnoverPie(year);
            }
        } else {
            //统计指定年月
            if (profitOrSell == 1) {
                //统计利润
                option = statisticsService.statisticsProfitPie(year, month);
            } else {
                //统计营业额
                option = statisticsService.statisticsTurnoverPie(year, month);
            }
        }
        System.out.println(option);
        return option;
    }
}
